/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-17 14:00:30
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.BatchUploadResult;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.edu.bean.EduCertUserInfoDto;
import com.joysuch.wwyt.edu.bean.EduCertUserQueryBean;
import com.joysuch.wwyt.edu.bean.EduCertificateUserEditBean;
import com.joysuch.wwyt.edu.bean.EduCertificateUserListBean;
import com.joysuch.wwyt.edu.bean.EduCertificateUserListBean.SearchEduCertificateUser;
import com.joysuch.wwyt.edu.service.EduCertificateUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/edu/certificate-user")
@Api(tags = "/edu/certificate-user 教育-证书-人员")
public class EduCertificateUserController {

    private static final Logger logger = LoggerFactory.getLogger(EduCertificateUserController.class);

    @Autowired
    private EduCertificateUserService eduCertificateUserService;

    @RequiresPermissions("edu:certificate:user:add")
    @RequestMapping("add")
    @ApiOperation("添加")
    public Object add(@Valid @RequestBody EduCertificateUserEditBean entity) {
        eduCertificateUserService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:certificate:user:update")
    @RequestMapping("update")
    @ApiOperation("修改")
    public Object update(@Valid @RequestBody EduCertificateUserEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        eduCertificateUserService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("edu:certificate:user:update")
    @RequestMapping("batch-upload-cert-image")
    @ApiOperation("批量导入证书照片")
    @ResponseBody
    public Object batchUploadCertImages(MultipartFile file) {
        BatchUploadResult result = eduCertificateUserService.batchUploadImages(file);
        return ResultBean.success(result);
    }

    // @RequiresPermissions("edu:certificate:user")
    @RequestMapping("page")
    @ApiOperation("分页查询")
    public Object page(Pageable page, SearchEduCertificateUser condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<EduCertificateUserListBean> data = eduCertificateUserService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    // @RequiresPermissions("edu:certificate:user")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchEduCertificateUser condition) {
        Page<EduCertificateUserListBean> data = eduCertificateUserService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("edu:certificate:user:delete")
    @RequestMapping("delete")
    @ApiOperation("删除")
    public Object delete(Long id) throws Exception {
        eduCertificateUserService.delete(id);
        return ResultBean.success("");
    }

    // @RequiresPermissions("edu:certificate:user")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(eduCertificateUserService.getById(id));
    }

    @RequestMapping("certPage")
    @ApiOperation("作业证模块-人员证书模板分页查询")
    public ResultBean certPage(EduCertUserQueryBean bean, Pageable pageable) {
        return eduCertificateUserService.certPage(bean, pageable);
    }

    @GetMapping("web-user-info-cert")
    @ApiOperation(value = "web端人员详情-资质证书查询 userContractorFlag: 0 人员; 1 承包商人员", response = EduCertUserInfoDto.class)
    public ResultBean userInfoCert(Pageable pageable, Long userId, Integer userContractorFlag) {
        return eduCertificateUserService.userInfoCert(pageable, userId, userContractorFlag);
    }

}
